<?php
namespace app\index\controller;

use library\Controller;
use app\index\service\ExportService;
use think\Db;

/**
 * 社保服务
 * Class Socsecs
 * @package app\index\controller
 */
class Socsecs extends Base
{
    /**
     * 社保服务列表
     */
    public function index()
    {
    	$contract = Db::name('system_contract')->where(['company_id'=>$this->company['id']])->order('id desc')->field('id,status,end_date,setmeal_type,people')->find();
    	if(!$contract){			
			$contract_type = 0;    
		}else{
			$contract_type = 1;
		}
    	$where[] = ['h.company_id','eq',$this->company['id']];
		$where[] = ['h.is_deleted','eq',0];
    	$type = $this->request->get('type');
        if(!$type){
            $where[] = ['h.type','eq',1];
        }else{
        	$where[] = ['h.type','eq',$type];
        } 
		$status =  $this->request->get('status');
		if($status){
			if($status == 1){
				$where[] = ['h.status','in',[1,2]];
			}
			if($status == 2){
				$where[] = ['h.status','eq',3];
			}
			if($status == 3){
				$where[] = ['h.status','eq',4];
			}
		} 
		$search =  $this->request->get('search');
		if($search){
			$where[] = ['h.code|h.name|h.id_number|s.title','like','%'.$search.'%'];
		} 
    	$list = Db::name('system_service_handle')
				->alias('h')
				->join('system_service s','h.service_id = s.id')
				->where($where)
				->field('h.id,h.code,h.name,h.id_number,h.status,h.create_at,h.point,s.title')
    			->order('h.id desc')
    			->paginate(10,false,['query'=>request()->param(),'type' => 'page\Page','var_page'  => 'page']);
		$this ->list = $list;
		$this ->contract_type = $contract_type; 	
        $this->fetch();
    }

	/**
     * 增值服务列表
     */
    public function services()
    {
    	$list = Db::name('system_service')->where(['type'=>2,'is_deleted' =>'0','status'=>1])->select();
		if($list){
			foreach($list as $k=>$v){
				$list[$k]['class'] = $v['class'] ? get_option('service_class_increment')[$v['class']] : '';
				$list[$k]['target'] = $v['target'] == 1 ? '公司' : '员工';
			}
		}
		$this ->list = $list;
		$contract = Db::name('system_contract')->where(['company_id'=>$this->company['id'],'status'=>1])->order('id desc')->field('id,status,end_date,setmeal_type,people')->find();
    	if($contract){
			$this ->setmeal_type = $contract['setmeal_type']; 
		}else{
			$this ->setmeal_type = $this->company['setmeal_type']; 
		}
        $this->fetch();  
    }

	/**
     * 导出公司服务
     */
    public function export_company()
    {
        $data = $this->request->param();
    	$ids = explode(',', $data['id']);
		$list = ExportService::export_handle_company($ids,$this->company['id']);
    	toexcel($list['filename'],$list['titles'],$list['list']);  
    }
	
	
	/**
     * 导出员工服务
     */
    public function export_person()
    {
        $data = $this->request->param();		
    	$ids = explode(',', $data['id']);
		$list = ExportService::export_handle_person($ids,$this->company['id']);
    	toexcel($list['filename'],$list['titles'],$list['list']);  
    }
	
}
